package com.berry.MedCheck;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.net.wifi.p2p.WifiP2pDevice;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.*;
import com.berry.MedCheck.db.DBMonitorService;
import com.berry.MedCheck.db.DBOperatorTest;
import com.berry.MedCheck.db.UserDB;

/**
 * Created with IntelliJ IDEA.
 * User: Airay.Rong
 * Date: 13-12-3
 * Time: 下午5:08
 * To change this template use File | Settings | File Templates.
 */
public class ChooseUserActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {
	private ListView mUserList;
	private Button mBtnAddNew;
	private Cursor mCursor;
	private SimpleCursorAdapter mAdapter;

	private static final String[] PROJECTION = new String[]{
			UserDB.DBColumns._ID, // 0
			UserDB.DBColumns.USERNAME, // 2

	};
	private static final int[] viewIDs = {
			R.id.dbid,
			R.id.name,
	};

	@Override
	protected void onResume() {
		super.onResume();
		Intent intent = getIntent();
		if (intent != null) {
			if (getIntent().getBooleanExtra("show_menu_btn", false) == false) {
				showMenuButton(false) ;
			}
		}
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		setContentView(R.layout.choose_user_activity);
		setupMenuCallback();
		mUserList = (ListView) findViewById(R.id.userlist);
		mUserList.setOnItemClickListener(this);
		mUserList.setDivider(null);
		mBtnAddNew = (Button) findViewById(R.id.btn_add_new);
		mBtnAddNew.setOnClickListener(this);

		mCursor = managedQuery(
				UserDB.DBColumns.CONTENT_URI,            // Use the default content URI for the provider.
				PROJECTION,                       // Return the note ID and title for each note.
				null,                             // No where clause, return all records.
				null,                             // No where clause, therefore no where column values.
				UserDB.DBColumns.DEFAULT_SORT_ORDER  // Use the default sort order.
		);

		mAdapter = new SimpleCursorAdapter(this, R.layout.username_item, mCursor, PROJECTION, viewIDs);
		mUserList.setAdapter(mAdapter);


	}

	@Override
	public void onClick(View v) {
		if (v == mBtnAddNew) {
			startActivity(new Intent().setClass(this, AddNewUserActivity.class));
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		//id 是数据库索引ID
		writePreference_CurrentUserName(id);
		startService(new Intent()
				.setClass(this, DBMonitorService.class));

		startActivity(new Intent()
				.setClass(this, DataActivity.class)
				.putExtra(Utils.INTENT_PARA_USER_ID, id)
				.setAction(Utils.INTENT_ACK_SHOW_DATA));
		finish();

	}

	//	private boolean readPreference_CurrentUserName(){
//		SharedPreferences sp = getSharedPreferences(Utils.PREFERENCE_NAME, 0);
//		boolean bAgree = sp.getBoolean(Utils.PRE_KEY_CURRENT_USER_ID,false);
//		return bAgree;
//	}
	private void writePreference_CurrentUserName(long userID) {
		SharedPreferences sp = getSharedPreferences(Utils.PREFERENCE_NAME, 0);
		SharedPreferences.Editor me = sp.edit();
		me.putLong(Utils.PRE_KEY_CURRENT_USER_ID, userID);
		me.commit();
	}
	private void setupMenuCallback() {
		mListener = new MenuCallback() {
			@Override
			public void onMenuClick(int id) {
				finish();
			}
		};
	}
}